package com.example.children.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.children.DTO.AttendanceTeacherDTO;
import com.example.children.DTO.ClassDTO;
import com.example.children.DTO.HeadToClassDTO;
import com.example.children.DTO.TeacherClassDTO;
import com.example.children.Exception.AttendanceException;
import com.example.children.Exception.ClassException;
import com.example.children.Exception.LoginException;
import com.example.children.Model.*;
import com.example.children.Model.Class;
import com.example.children.Service.ClassService;
import com.example.children.Util.Tools;
import com.example.children.Util.UUIDGenerator;
import com.example.children.Util.tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Class")
public class ClassController {

    @Autowired
    private ClassService classService;

    // 分页所有出勤记录
    @GetMapping("/All")
    public ResponseModel<Page<ClassDTO>> getAttendanceByStudent(
            @RequestParam(defaultValue = "1") int pageNo,  // 默认页码为1
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        System.out.println("进入查询班级All方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
        Page<ClassDTO> classPage=classService.GetClassPage(pageNo,pageSize);
        if(classPage == null || classPage.getRecords().isEmpty()){
            System.out.println("没有班级数据");
            msg= tool.fail;
        }
        return new ResponseModel<>(classPage,code,msg,null);//响应给前端的对象
    }



    // 添加学生
    @PostMapping("/addStudentToClass")
    public ResponseModel<HeadToClassDTO> addStudentToClass(@RequestBody HeadToClassDTO headToClassDTO) {
        System.out.println("进入addStudentToClass方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
        if(classService.IsSaveStudent(headToClassDTO))
            throw new ClassException("该学生已添加在其他班级");
        try {
            // 执行保存操作
            if (classService.addStudentToClass(headToClassDTO)) {
                return new ResponseModel<>(headToClassDTO, code, msg, null); // 成功响应
            } else {
                throw new AttendanceException("保存失败！！！");
            }
        } catch (DataAccessException e) {
            // 捕获 Spring 的数据库异常
            throw new AttendanceException("数据库操作失败，请稍后再试。"+e, e);
        } catch (Exception e) {
            // 捕获其他类型的异常
            throw new AttendanceException("发生未知错误，请稍后再试。"+e, e);
        }
    }


    // 添加老师
    @PostMapping("/addTeacherToClass")
    public ResponseModel<HeadToClassDTO> addTeacherToClass(@RequestBody HeadToClassDTO headToClassDTO) {
        System.out.println("进入addTeacherToClass方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
        if(classService.IsSaveTeacher(headToClassDTO))
            throw new ClassException("该老师已添加在其他班级");
        try {
            // 执行保存操作
            if (classService.addStudentToClass_two(headToClassDTO)) {
                return new ResponseModel<>(headToClassDTO, code, msg, null); // 成功响应
            } else {
                throw new AttendanceException("保存失败！！！");
            }
        } catch (DataAccessException e) {
            // 捕获 Spring 的数据库异常
            throw new AttendanceException("数据库操作失败，请稍后再试。"+e, e);
        } catch (Exception e) {
            // 捕获其他类型的异常
            throw new AttendanceException("发生未知错误，请稍后再试。"+e, e);
        }
    }

    @PostMapping("/add")
    public ResponseModel<Class> AddClass(@RequestBody Class ct) {
        System.out.println(ct.getClassId()+"进入add方法：" + ct.getClassName());
        Integer code = Tools.Success;
        String msg = tool.success;
        String id=UUIDGenerator.generateNumericUUID(6);
        ct.setClassId(id);
        classService.insertClass(ct);
        return new ResponseModel<>(ct, code, msg, null);//响应给前端的对象
    }

    @PostMapping("/update")
    public ResponseModel<Class> updateStudent(@RequestBody Class ct) {
        System.out.println(ct.getClassId()+"进入updateTeacher方法：" + ct.getClassName());
        Integer code = Tools.Success;
        String msg = tool.success;
        classService.update(ct);
        return new ResponseModel<>(ct, code, msg, null);//响应给前端的对象
    }

    @DeleteMapping("/delete")  //删除
    public ResponseModel<Class> deleteTeacher(@RequestParam String id) {
        System.out.println("进入deleteTeacher方法：" + id);
        Integer code = Tools.Success;
        String msg = tool.success;
        classService.delete(id);
        return new ResponseModel<>(null, code, msg, null);//响应给前端的对象

    }

    //查询
    @PostMapping("/ManySelectClass")
    public ResponseModel<List<TeacherClassDTO>> ManySelectClass(@RequestBody TeacherClassDTO teacherClassDTO) {
        System.out.println("ManySelectStudent");
        Integer code = Tools.Success;
        String msg = tool.success;
        List<TeacherClassDTO> students = classService.ManySelectClass(teacherClassDTO);
        if (students == null || students.isEmpty())
            msg = tool.fail;
        return new ResponseModel<>(students, code, msg, null); // 成功响应
    }


}
